package src.java.difficulty;

public class Solution135 {
    public static boolean isMax(int[] ratings, int i) {
        if (i != 0 && i != ratings.length - 1) {
            if (ratings[i] > ratings[i - 1] && ratings[i] > ratings[i + 1])
                return true;
        }
        return false;
    }

    public int candy(int[] ratings) {
        int len = ratings.length;
        int sum = len;
        for (int i = 1; i < len; i++) {
            if (ratings[i] != ratings[i - 1]) {
                if (!isMax(ratings, i)) sum++;
            }
        }
        return sum;
    }
}
